package com.heima.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.search.vos.SearchArticleVo;
import com.heima.search.service.ApArticleSearchService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ApArticleSearchServiceImpl implements ApArticleSearchService {
    @Autowired
    private RestHighLevelClient client;

    /**
     * 加载文章
     * @param dto
     * @return
     */
    @Override
    public List<ApArticle> load(ArticleHomeDto dto) {
        try {
            // 1.校验参数
            if (dto == null || dto.getType() == null) {
                return null;
            }
            // 2.设置条件
            SearchRequest searchRequest = new SearchRequest("app_info_article");
            BoolQueryBuilder bool = QueryBuilders.boolQuery();
            // 加载更多
            if (dto.getType() == 1) {
                bool.filter(QueryBuilders.rangeQuery("publishTime").lt(dto.getMinBehotTime()));
            }
            // 加载最新
            if (dto.getType() == 2) {
                bool.filter(QueryBuilders.rangeQuery("publishTime").gt(dto.getMaxBehotTime()));
            }
            // 加载频道
            if (!"__all__".equals(dto.getTag())) {
                bool.filter(QueryBuilders.termQuery("channelId", dto.getTag()));
            }
            searchRequest.source().query(bool).size(dto.getSize()).sort("publishTime", SortOrder.DESC);
            // 3.响应结果封装
            SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHit[] hits = response.getHits().getHits();
            List<ApArticle> articleList = new ArrayList<>();
            if (hits != null && hits.length > 0) {
                for (SearchHit hit : hits) {
                    String source = hit.getSourceAsString();
                    SearchArticleVo vo = JSON.parseObject(source, SearchArticleVo.class);
                    ApArticle apArticle = new ApArticle();
                    BeanUtils.copyProperties(vo, apArticle);
                    apArticle.setLayout(Short.valueOf(vo.getLayout().toString()));
                    articleList.add(apArticle);
                }
            }
            // 4.返回结果
            return articleList;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
